package com.example.yls.demoa;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import uk.co.imallan.jellyrefresh.JellyRefreshLayout;
import uk.co.imallan.jellyrefresh.PullToRefreshLayout;

public class JellyActivity extends AppCompatActivity {
    private static final int MY_RE = 1001 ;
    private JellyRefreshLayout jellyRefreshLayout;
    private ListView listView;
    private ArrayList<String> data = new ArrayList<>();
    private ArrayAdapter arrayAdapter;
    private Handler handler;
    private int j = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jelly);

        setTitle("Jelly");

        initHandler();

        jellyRefreshLayout = findViewById(R.id.jellyLayout);
        listView = findViewById(R.id.mylist);

        arrayAdapter = new ArrayAdapter(JellyActivity.this,
                android.R.layout.simple_list_item_1,data);
        arrayAdapter.add("111");

        listView.setAdapter(arrayAdapter);

        jellyRefreshLayout.setPullToRefreshListener(new PullToRefreshLayout.PullToRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        for(int i=0; i<1; i++){
                            data.add("刷新"+j+"次");
                        }
                        j++;
                        handler.sendEmptyMessage(MY_RE);
                    }
                }).start();
//
            }
        });

//        swipeRefreshLayout.setPullToRefreshListener(new );

//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//
//
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(3000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        for(int i=0; i<1; i++){
//                            data.add("刷新"+j+"次");
//                        }
//                        j++;
//                        handler.sendEmptyMessage(MY_RE);
//                    }
//                }).start();
//
//            }
//        });

    }

    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == MY_RE){
                    arrayAdapter.notifyDataSetChanged();
                    jellyRefreshLayout.setRefreshing(false);
                }
                return false;
            }
        });


    }
}
